<template>
  <div class="aui-content">
    <div class="aui-container">
      <div class="aui-form">
        <div class="aui-formBox">
          <div class="aui-formWell">
            <div class="download_tit">移动应用二维码</div>
            <div class="smxz_box">
              <div class="smxz_item">
                <QrCode class="downewm_img" :width="120" :value="androidUrl" :options="{margin: 2}" />
                <div class="bb_btn" @click="downloadAndroid">
                  <img class="bb_icon" src="/src/assets/loginmini/icon/bb_icon2.png" alt="">
                  <span>Android版</span>
                </div>
              </div>
              <div class="smxz_item">
                <QrCode class="downewm_img" :width="120" :value="androidUrl" :options="{margin: 2}" />
                <div class="bb_btn" @click="downloadIos">
                  <img class="bb_icon" src="/src/assets/loginmini/icon/bb_icon1.png" alt="">
                  <span>iphone版</span>
                </div>
              </div>
            </div>
            <div class="aui-formButton" style="padding-bottom: 40px">
              <div class="aui-flex">
                <a class="aui-linek-code aui-flex-box" @click="goBack"> {{ t('sys.login.backSignIn') }}</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" name="mini-forgotpad" setup>
  import { reactive, ref, toRaw, unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { SmsEnum, useFormRules, useFormValid, useLoginState } from '/@/views/sys/login/useLogin';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { QrCode, QrCodeActionType } from '/@/components/Qrcode/index';

  //下一步控制
  const activeKey = ref<number>(1);
  const { t } = useI18n();
  const { handleBackLogin } = useLoginState();
  const { notification, createMessage, createErrorModal } = useMessage();
  //是否显示获取验证码
  const showInterval = ref<boolean>(true);
  //60s
  const timeRuning = ref<number>(60);
  //定时器
  const timer = ref<any>(null);
  const formRef = ref();
  const pwdFormRef = ref();
  //账号数据
  const accountInfo = reactive<any>({});
  //手机号表单
  const formData = reactive({
    mobile: '',
    smscode: '',
  });
  //密码表单
  const pwdFormData = reactive<any>({
    password: '',
    confirmPassword: '',
  });
  const emit = defineEmits(['go-back', 'success', 'register']);
  
  const androidUrl = ref("https://minio.cng-zh.com/miniotraceability/app/nlqxapp.apk")
  
  const downloadAndroid = () => {
    window.open(androidUrl.value);
  }
  
  const downloadIos = () => {
    notification.error({
      message: '温馨提示',
      description: "暂无",
      duration: 3,
    });
  }

  /**
   * 下一步
   */
  async function handleNext() {
    if (!formData.mobile) {
      createMessage.warn(t('sys.login.mobilePlaceholder'));
      return;
    }
    if (!formData.smscode) {
      createMessage.warn(t('sys.login.smsPlaceholder'));
      return;
    }
    const resultInfo = await phoneVerify(
      toRaw({
        phone: formData.mobile,
        smscode: formData.smscode,
      })
    );
    if (resultInfo.success) {
      Object.assign(accountInfo, {
        username: resultInfo.result.username,
        phone: formData.mobile,
        smscode: formData.smscode,
      });
      activeKey.value = 2;
      setTimeout(()=>{
        // pwdFormRef.value.resetFields();
      },300)
    } else {
      notification.error({
        message: '错误提示',
        description: resultInfo.message || t('sys.api.networkExceptionMsg'),
        duration: 3,
      });
    }
  }

  /**
   * 完成修改密码
   */
  async function finishedPwd() {
    if (!pwdFormData.password) {
      createMessage.warn(t('sys.login.passwordPlaceholder'));
      return;
    }
    if (!pwdFormData.confirmPassword) {
      createMessage.warn(t('sys.login.confirmPassword'));
      return;
    }
    if (pwdFormData.password !== pwdFormData.confirmPassword) {
      createMessage.warn(t('sys.login.diffPwd'));
      return;
    }
    const resultInfo = await passwordChange(
      toRaw({
        username: accountInfo.username,
        password: pwdFormData.password,
        smscode: accountInfo.smscode,
        phone: accountInfo.phone,
      })
    );
    if (resultInfo.success) {
      accountInfo.password = pwdFormData.password;
      //修改密码
      activeKey.value = 3;
    } else {
      //错误提示
      createErrorModal({
        title: t('sys.api.errorTip'),
        content: resultInfo.message || t('sys.api.networkExceptionMsg'),
      });
    }
  }
  /**
   * 下一步
   */
  function nextStepClick() {
    if (unref(activeKey) == 1) {
      handleNext();
    } else if (unref(activeKey) == 2) {
      finishedPwd();
    }
  }

  /**
   * 去登录
   */
  function toLogin() {
    emit('success', { username: accountInfo.username, password: accountInfo.password });
    initForm();
  }

  /**
   * 返回
   */
  function goBack() {
    emit('go-back');
    initForm();
  }

  /**
   * 获取手机验证码
   */
  async function getLoginCode() {
    if (!formData.mobile) {
      createMessage.warn(t('sys.login.mobilePlaceholder'));
      return;
    }
    const result = await getCaptcha({ mobile: formData.mobile, smsmode: SmsEnum.FORGET_PASSWORD });
    if (result) {
      const TIME_COUNT = 60;
      if (!unref(timer)) {
        timeRuning.value = TIME_COUNT;
        showInterval.value = false;
        timer.value = setInterval(() => {
          if (unref(timeRuning) > 0 && unref(timeRuning) <= TIME_COUNT) {
            timeRuning.value = timeRuning.value - 1;
          } else {
            showInterval.value = true;
            clearInterval(unref(timer));
            timer.value = null;
          }
        }, 1000);
      }
    }
  }

  /**
   * 初始化表单
   */
  function initForm() {
    activeKey.value = 1;
    Object.assign(formData, { phone: '', smscode: '' });
    Object.assign(pwdFormData, { password: '', confirmPassword: '' });
    Object.assign(accountInfo, {});
    if(unref(timer)){
      clearInterval(unref(timer));
      timer.value = null;
      showInterval.value = true;
    }
    setTimeout(()=>{
      // formRef.value.resetFields();
    },300)
  }

  defineExpose({
    initForm,
  });
</script>
<style lang="less" scoped>
  @import '/@/assets/loginmini/style/home.less';
  
  .download_tit {
    text-align: center;
    font-size: 18px;
    font-weight: 500;
    color: #040404;
    margin-bottom: 20px;
  }
  .smxz_box {
    display: flex;
    justify-content: center;
    padding-top: 30px;
    margin-bottom: 40px;
  }
  .smxz_item {
    text-align: center;
    // font-size: 12px;
    margin: 0px 15px;
    max-width: 120px;
    overflow: hidden;
  }
  .downewm_img {
    width: 120px;height: 120px;margin: auto;
    background: #DAE0E8;
    display: block;
    margin-bottom: 10px;
  }
  .bb_icon {
    width: 20px;
    height: 20px;margin: auto;
    // display: block;
  }
  .bb_btn {
    margin: auto;
    width: 100px;
    height: 34px;border: 1px solid #058D5E;
    border-radius: 4px;
    font-size: 12px;
    line-height: 30px;
    cursor: pointer;
  }
</style>
